package com.hnqcgczy.dao.imp;

import com.hnqcgczy.dao.BaseDao;
import com.hnqcgczy.dao.IShopCartDao;
import com.hnqcgczy.db.MyTransaction;
import com.hnqcgczy.vo.ShopCartVo;
import com.hnqcgczy.vo.TShopcart;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 * 完成购物车Dao 的新增，修改，删除，查询等功能
 */
public class ShopCartDaoImpl extends BaseDao implements IShopCartDao {

    //事务定义
    MyTransaction myTransaction;
    //构造方法， 完成类的初始化
    public ShopCartDaoImpl(MyTransaction myTransaction){
        //给父类BaseDao 初始化
        super(myTransaction);
        //给自己的属性赋值
        this.myTransaction=myTransaction;
    }
    @Override
    public int addShopCart(TShopcart cart) {
        //1 编写预编译SQL语句
        String sql="insert into t_shopcart values(null,?,?,?)";
        //2 调用baseDao统一新增方法
        int count=this.update(sql,cart.getTuId(),cart.getTpId(),cart.getTsCount());
        //3 释放资源
        this.closeResource();
        //4 返回结果
        return count;
    }

    @Override
    public int delShopCart(long tsId) {
        // 1预编译sql
        String sql="delete from t_shopcart where ts_id =?";
        //2 调用baseDao执行
        int count=this.update(sql,tsId);
        //3 释放资源
        this.closeResource();
        //4 返回结果
        return count;
    }

    @Override
    public int updateShopCart(TShopcart cart) {
        //1 预编译SQL语句
        String sql="update t_shopcart set ts_count =? where ts_id =?";
        //2 执行
        int count =this.update(sql,cart.getTsCount(),cart.getTsId());
        //3 释放资源
        this.closeResource();
        //4 返回结果
        return count;
    }

    @Override
    public int delShopCartByTuId(String tuId) {
        //1 预编译SQL语句
        String sql="delete from t_shopcart where tu_id =?";
        //2 执行
        int count =this.update(sql,tuId);
        //3 释放资源
        this.closeResource();
        //4 返回结果
        return count;
    }

    @Override
    public TShopcart getShopCartByTuIdAndTpId(String tuId, long tpId) {
        //1 预编译SQL
        String sql ="select * from t_shopcart where tu_id =? and tp_id =?";
        //2 使用DBUtils查询
        QueryRunner queryRunner=new QueryRunner();
        // 3处理下划线转换成驼峰命名属性
        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);
        //4 定义返回对象
        TShopcart cart=null;
        //5执行查询，获得返回对象
        try {
            cart=queryRunner.query(myTransaction.getConn(),sql,
                    new BeanHandler<TShopcart>(TShopcart.class,rowProcessor),tuId,tpId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //6返回结果
        return cart;
    }

    @Override
    public List<ShopCartVo> getShopCartList(String tuId) {
        //1 SQL
        String sql ="select * from t_shopcart cart,t_product p where cart.tp_id=p.tp_id and cart.tu_id=?";
        //2 DBUtils完成查询
        QueryRunner queryRunner=new QueryRunner();
        //3： 处理下划线转换成驼峰命名
        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);
        //4 定义返回结果
        List<ShopCartVo> list=new ArrayList<ShopCartVo>();
        //5 执行查询
        try {
            list=queryRunner.query(myTransaction.getConn(),sql,
                    new BeanListHandler<ShopCartVo>(ShopCartVo.class,rowProcessor),tuId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //6 返回结果
        return  list;
    }
}
